home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / pcscheme / geneva / sources.exe / SOURCES / ASM / START.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-09-30  |  7.8 KB  |  318 lines

  1. ;* START.ASM
  2. ;************************************************************************
  3. ;*                                    *
  4. ;*        PC Scheme/Geneva 4.00 Borland TASM code            *
  5. ;*                                    *
  6. ;* (c) 1985-1988 by Texas Instruments, Inc. See COPYRIGHT.TXT        *
  7. ;* (c) 1992 by L. Bartholdi & M. Vuilleumier, University of Geneva    *
  8. ;*                                    *
  9. ;*----------------------------------------------------------------------*
  10. ;*                                    *
  11. ;*        Startup & Exit code for Borland C            *
  12. ;*                                    *
  13. ;*----------------------------------------------------------------------*
  14. ;*                                    *
  15. ;* Created by: John Jensen        Date: 1985            *
  16. ;* Revision history:                            *
  17. ;* - 18 Jun 92:    Renaissance (Borland Compilers, ...)            *
  18. ;*                                    *
  19. ;*                    ``In nomine omnipotentii dei''    *
  20. ;************************************************************************
  21. IDEAL
  22. %PAGESIZE    60, 132
  23. MODEL    medium
  24. LOCALS    @@
  25.  
  26.     INCLUDE    "scheme.ash"
  27.  
  28. DATASEG
  29.  
  30.     EXTRN    C _psp:word, C _argc:word, C _argv:word
  31.  
  32. prev_mode DB    ?, 0
  33.  
  34. ; the characters sought by PCS's scanner
  35. scan_table    DB 0dh, 9, ' ()"', ?
  36. SCANSIZE = $-scan_table
  37.  
  38. ; the FSA transition table used to parse PCS's command line
  39. ; (This once included handling for vertical-bar delimited symbols, but
  40. ; DOS's use of | rendered it useless, so it was removed.)
  41.  
  42. STRUC    TRANSITION
  43. tstate    DB    ?
  44. taction    DB    ?
  45. ENDS    TRANSITION
  46.  
  47. MACRO    TRANS    st, act
  48.     DB    st
  49.     DB    act-pcsparse
  50. ENDM
  51.  
  52. LABEL    state    TRANSITION
  53. STARTSTATE = 0
  54.     TRANS    ATOMSTATE, ar_start    ; any char
  55.     TRANS    STRINGSTATE, ar_start    ; "
  56.     TRANS    ERRORSTATE, ar_err    ; )
  57.     TRANS    LISTSTATE, ar_startl    ; (
  58.     TRANS    STARTSTATE, ar_skip    ; blank
  59.     TRANS    STARTSTATE, ar_skip    ; tab
  60.     TRANS    ENDSTATE, ar_end    ; CR (end of command line)
  61. BYTESSTATE = $-state
  62. LISTSTATE    =    1
  63.     TRANS    LISTSTATE, ar_loop
  64.     TRANS    LISTSTATE, ar_loop
  65.     TRANS    LISTSTATE, ar_rpar
  66.     TRANS    LISTSTATE, ar_lpar
  67.     TRANS    LISTSTATE, ar_loop
  68.     TRANS    LISTSTATE, ar_loop
  69.     TRANS    ERRORSTATE, ar_err
  70. ATOMSTATE    =    2
  71.     TRANS    ATOMSTATE, ar_loop
  72.     TRANS    ATOMSTATE, ar_loop
  73.     TRANS    ATOMSTATE, ar_loop
  74.     TRANS    ATOMSTATE, ar_loop
  75.     TRANS    STARTSTATE, ar_skip
  76.     TRANS    STARTSTATE, ar_skip
  77.     TRANS    ENDSTATE, ar_end
  78. STRINGSTATE    =     3
  79.     TRANS    STRINGSTATE, ar_loop
  80.     TRANS    STARTSTATE, ar_loop
  81.     TRANS    STRINGSTATE, ar_loop
  82.     TRANS    STRINGSTATE, ar_loop
  83.     TRANS    STRINGSTATE, ar_loop
  84.     TRANS    STRINGSTATE, ar_loop
  85.     TRANS    ERRORSTATE, ar_err
  86. ENDSTATE    =    4
  87. ERRORSTATE    =    5
  88.  
  89. ; The exit and error states are not explicitly represented
  90. ; in the table, action routines deal with them.
  91.  
  92. parserr    DB    "Error in parsing command line", 0dh, 0ah, "$"
  93.  
  94. CODESEG
  95.  
  96. ; -------------------------------------------------
  97. ; StartText - Ensure PCS start in text mode
  98. ; -------------------------------------------------
  99.  
  100. PROC C    starttext USES si di
  101.     mov    ah, 0fh            ; get current video mode
  102.     int    IBM_CRT
  103.     mov    [prev_mode], al        ; save it until PCS exit
  104.     call    is_graph_mode
  105.     or    ax, ax
  106.     jz    @@textmode
  107.     mov    ax, 7            ; Try monochrome mode
  108.     int    IBM_CRT
  109.     mov    ax, 3            ; Try CGA mode (the good'll work!)
  110.     int    IBM_CRT
  111. @@textmode:
  112.     call    zcuroff C        ; Turn cursor off (and remember size)
  113.     ret
  114. ENDP    starttext
  115.  
  116. ; -------------------------------------------------
  117. ; ExitText - Put back previous mode when PCS exit
  118. ; -------------------------------------------------
  119.  
  120. PROC C    exittext USES si di
  121.     mov    ah, 0fh            ; get current video mode
  122.     int    IBM_CRT
  123.     cmp    [prev_mode], al        ; same as on entry ?
  124.     jz    @@ret
  125.     mov    ax, [WORD prev_mode]    ; set video mode
  126.     int    IBM_CRT
  127. @@ret:
  128.     call    zcuron C         ; Turn cursor back on
  129.     ret
  130. ENDP    exittext
  131.  
  132. ; -------------------------------------------------
  133. ; The PCS command line parser
  134. ; -------------------------------------------------
  135. ; The PCS command line looks as follows:
  136. ;
  137. ;    PCS <arglist>
  138. ; where:
  139. ;    arglist ::= [<item> <arglist>]
  140. ;    item ::= (<arglist>) | <atom> | "<string>"
  141. ;    atom ::= <blackchar>[<atom>]
  142. ;    string ::= [<anychar><string>]
  143. ;    anychar ::= <blackchar> | white space
  144. ;    blackchar ::= a character | \<allchars>
  145. ;    allchars ::= absolutely anything, except null char
  146. ;
  147. ; eg.    PCS (abc "def") is (a (silly ("\"example for you\"")))
  148. ;
  149. ; which parses into PCS-INITIAL-ARGUMENTS as:
  150. ;
  151. ;    ("(abc \"def\")" "is" "(a (silly (\"\"example for you\"\")))"),
  152. ; when (let ((p (open-input-string (caddr pcs-initial-arguments))))
  153. ;    (string->atom (caddr (read p)))))
  154. ; would return "example for you".
  155. ;
  156. ; Each command line argument is either an atom, list, or string.
  157. ; Each is treated as one argument for the argv vector, and each is
  158. ; converted to a string which becomes an element of PCS-INITIAL-ARGUMENTS.
  159. ;
  160. ; The command line parser is not a Scheme reader.  It looks for blank-separated
  161. ; tokens, where a token can start with a ( and end with the matching ),
  162. ; start and end with a ", or just be a sequence of nonblanks.  Backslashed
  163. ; delimiters are skipped over as you'd expect.  We don't bother with | since
  164. ; that is a special character to DOS.
  165. ;
  166. ; The first command line argument has special meaning but that is
  167. ; handled in "smain.c".
  168. ;
  169. PROC C    pcsparse USES si di
  170.     mov    es, [_psp]
  171.     mov    si, 81h
  172.     mov    bl, [es:si-1]
  173.     mov    bh, 0
  174.     mov    [BYTE es:si+bx], 0dh; ensure command is CR-terminated
  175.     inc    bx
  176.     push    bx
  177. @@skipspace:
  178.     cmp    [BYTE es:si], ' '
  179.     je    @@found
  180.     cmp    [BYTE es:si], 9
  181.     jne    @@done
  182. @@found:
  183.     dec    bx
  184.     inc    si
  185.     jmp    @@skipspace
  186. @@done:
  187.     call    malloc C, bx
  188.  
  189.     push    ds es
  190.     pop    ds es
  191.  
  192.     or    ax, ax
  193.     je    ar_err
  194.     mov    di, ax
  195.     pop    cx
  196.     cld
  197.     rep    movsw            ; move the arguments to our new block
  198.     push    es
  199.     pop    ds
  200.  
  201.     mov    di, [_argv]
  202.     add    di, 2            ; leave argv[0] unchanged; es:di is argv
  203.     mov    si, ax            ; ds:si is command line
  204.     mov    ah, 0             ; ah is current state
  205.                     ; al is current character
  206.     mov    dx, 1             ; dh is parenthesis counter
  207.                     ; dl is argument count
  208.     cld
  209. ar_loop:
  210. @@loop:
  211.     lodsb
  212.     cmp    al, '\'            ; is it singly escaped?
  213.     jne    @@singlesc
  214.     cmp    [BYTE si], 0dh        ; end of cmdline ?
  215.     je    ar_err
  216.     inc    si
  217.     jmp    @@loop
  218. @@singlesc:
  219.     mov    cx, SCANSIZE         ; look it up in char table
  220.     push    di
  221.     lea    di, [scan_table]
  222.     repne    scasb             ; put into cx the "char class" for
  223.                     ; indexing into state table
  224.     pop    di
  225.     mov    al, BYTESSTATE        ; do 2-D subscript into state table
  226.     mul    ah             ; ... row
  227.     shl    cx, 1             ; ... col
  228.     add    ax, cx
  229.     mov    bx, ax             ; (bh=0 since subscript small enough)
  230.     mov    ah, [state+bx.tstate]
  231.     mov    bl, [state+bx.taction]
  232.     add    bx, OFFSET pcsparse
  233.     jmp    bx
  234.  
  235. ar_startl:
  236.     inc    dh
  237. ar_start:
  238.     push    ax
  239.     lea    ax, [si-1]
  240.     stosw
  241.     pop    ax
  242.     inc    dl
  243.     jmp    @@loop
  244. ar_lpar:
  245.     inc    dh             ; incr paren count
  246.     jmp    @@loop
  247. ar_rpar:
  248.     dec    dh             ; decr paren count
  249.     js    ar_err
  250.     jnz    @@loop
  251.     mov    ah, STARTSTATE        ; override state in table
  252.     jmp    @@loop
  253. ar_skip:
  254.     mov    [BYTE si-1], 0        ; output a null char
  255.     jmp    @@loop
  256. ar_err:
  257.     lea    dx, [parserr]        ; abort on error in cmdline parsing
  258.     mov    ah, 9
  259.     int    21h
  260.     mov    ax, 4cffh
  261.     int    21h
  262. ar_end:
  263.     xor    ax, ax
  264.     mov    [BYTE si-1], al        ; put a null here too
  265.     stosw                ; argv[argc] is NULL
  266.     mov    [_argc], dx
  267.     ret
  268. ENDP    pcsparse
  269.  
  270. ;
  271. ; Scheme wrapup - the C fn "exit" calls "_exit" which calls this hook routine
  272. ;
  273.  
  274. PROC C    pcsexit    USES si di
  275.     cmp    [mouse_use], 0
  276.     je    @@nomouse
  277.     mov    ax, 0            ; reset mouse handler
  278.     int    33h
  279.     mov    [mouse_use], 0        ; disable mouse
  280. @@nomouse:
  281.     push    es             ; return Scheme heap to DOS
  282.     mov    ah, 49h
  283.     mov    es, [first_dos]
  284.     int    MSDOS
  285.     pop    es
  286.     mov    dx, [emshandle]
  287.     cmp    dx, 0ffffh
  288.     je    @@noems
  289.     mov    ah, 45h            ; release EMS handle
  290.     int    EMMINT
  291. @@noems:
  292.     call    rsttimer C         ; Reset the timer interrupt, if necessary
  293.     call    unfixint C         ; Restore the keyboard "patch" (MWH2)
  294.     ret
  295. ENDP    pcsexit
  296.  
  297. ; Installation of the startup/exit code (#pragma startup/exit)
  298.  
  299. SEGMENT    _INIT_    word public 'INITDATA'
  300.     DB    1            ; 1 = far, 0 = near
  301.     DB    100            ; default priority
  302.     DD    starttext
  303.     DB    1            ; 1 = far, 0 = near
  304.     DB    100            ; default priority
  305.     DD    pcsparse
  306. ENDS    _INIT_
  307.  
  308. SEGMENT    _EXIT_    word public 'EXITDATA'
  309.     DB    1            ; 1 = far, 0 = near
  310.     DB    100            ; default priority
  311.     DD    exittext
  312.     DB    1            ; 1 = far, 0 = near
  313.     DB    100            ; default priority
  314.     DD    pcsexit
  315. ENDS    _EXIT_
  316.  
  317.     END
  318.